package com.istock.monitor.agent.spring;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import com.istock.monitor.agent.sql.MyDataSource;

public class DataSourceBeanPostProcessor implements BeanPostProcessor {

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		if (javax.sql.DataSource.class.isAssignableFrom(bean.getClass())) {
			return createDataSourceProxyInstance(bean, beanName);
		}
		
		
		
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName)
			throws BeansException {
		if (javax.sql.DataSource.class.isAssignableFrom(bean.getClass())) {
			return createDataSourceProxyInstance(bean, beanName);
		}
		
		
		
		return bean;
	}

	
	private Object createDataSourceProxyInstance(Object bean, String beanName) {
		System.out.println("===================================dataSource proxy invoke==============");
		if (bean instanceof MyDataSource) {
			return ((MyDataSource) bean).getDataSource();
		}else{
			return new MyDataSource((javax.sql.DataSource) bean) ;
		}
		//MyDataSource dataSource = new MyDataSource((javax.sql.DataSource) bean);
		//return dataSource;
		
	}
}
